{
 "metadata": {
  "name": "beginner"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Beginner Tutorial for SymPy Mechanics"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Once you are at the python command line the first step is to import basic functionality from SymPy and the Mechanics module, otherwise you will only have basic python commands available to work with. We will import the `symbols` function from SymPy core and with the * method\n",
      "bring in all functionality from the mechanics package."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import symbols\n",
      "from sympy.physics.mechanics import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "You can now see what functions and variables that are available to you with::"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dir()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "['Dyadic',\n",
        " 'In',\n",
        " 'KanesMethod',\n",
        " 'LagrangesMethod',\n",
        " 'Lagrangian',\n",
        " 'MechanicsLatexPrinter',\n",
        " 'MechanicsPrettyPrinter',\n",
        " 'MechanicsStrPrinter',\n",
        " 'Out',\n",
        " 'Particle',\n",
        " 'Point',\n",
        " 'ReferenceFrame',\n",
        " 'RigidBody',\n",
        " 'Vector',\n",
        " '_',\n",
        " '__',\n",
        " '___',\n",
        " '__builtin__',\n",
        " '__builtins__',\n",
        " '__name__',\n",
        " '__package__',\n",
        " '_dh',\n",
        " '_i',\n",
        " '_i1',\n",
        " '_i2',\n",
        " '_ih',\n",
        " '_ii',\n",
        " '_iii',\n",
        " '_oh',\n",
        " '_sh',\n",
        " 'angular_momentum',\n",
        " 'cross',\n",
        " 'dot',\n",
        " 'dynamicsymbols',\n",
        " 'exit',\n",
        " 'express',\n",
        " 'get_ipython',\n",
        " 'help',\n",
        " 'inertia',\n",
        " 'inertia_of_point_mass',\n",
        " 'kinematic_equations',\n",
        " 'kinetic_energy',\n",
        " 'linear_momentum',\n",
        " 'mechanics_printing',\n",
        " 'mlatex',\n",
        " 'mpprint',\n",
        " 'mprint',\n",
        " 'outer',\n",
        " 'partial_velocity',\n",
        " 'potential_energy',\n",
        " 'quit',\n",
        " 'symbols']"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This is a long list of available functions. Read about the python import statement to learn about better ways to import only\n",
      "what you need. One good explanation is <http://effbot.org/zone/import-confusion.htm>.\n",
      "    \n",
      "To get started working with vectors we will need to create a reference frame,\n",
      "as all vectors need to be defined with respect to a reference frame in the mechanics package. If you\n",
      "know the name of the command that you want to use simply use the builtin help\n",
      "function to bring up the documentation for the function. In our case we need to\n",
      "use the ReferenceFrame class."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "help(ReferenceFrame)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Help on class ReferenceFrame in module sympy.physics.mechanics.essential:\n",
        "\n",
        "class ReferenceFrame(__builtin__.object)\n",
        " |  A reference frame in classical mechanics.\n",
        " |  \n",
        " |  ReferenceFrame is a class used to represent a reference frame in classical\n",
        " |  mechanics. It has a standard basis of three unit vectors in the frame's\n",
        " |  x, y, and z directions.\n",
        " |  \n",
        " |  It also can have a rotation relative to a parent frame; this rotation is\n",
        " |  defined by a direction cosine matrix relating this frame's basis vectors to\n",
        " |  the parent frame's basis vectors.  It can also have an angular velocity\n",
        " |  vector, defined in another frame.\n",
        " |  \n",
        " |  Methods defined here:\n",
        " |  \n",
        " |  __getitem__(self, ind)\n",
        " |      Returns basis vector for the provided index (index being an str)\n",
        " |  \n",
        " |  __init__(self, name, indices=None, latexs=None)\n",
        " |      ReferenceFrame initialization method.\n",
        " |      \n",
        " |      A ReferenceFrame has a set of orthonormal basis vectors, along with\n",
        " |      orientations relative to other ReferenceFrames and angular velocities\n",
        " |      relative to other ReferenceFrames.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      indices : list (of strings)\n",
        " |          If custom indices are desired for console, pretty, and LaTeX\n",
        " |          printing, supply three as a list. The basis vectors can then be\n",
        " |          accessed with the get_item method.\n",
        " |      latexs : list (of strings)\n",
        " |          If custom names are desired for LaTeX printing of each basis\n",
        " |          vector, supply the names here in a list.\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, mlatex\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> N.x\n",
        " |      N.x\n",
        " |      >>> O = ReferenceFrame('O', ('1', '2', '3'))\n",
        " |      >>> O.x\n",
        " |      O['1']\n",
        " |      >>> O['1']\n",
        " |      O['1']\n",
        " |      >>> P = ReferenceFrame('P', latexs=('A1', 'A2', 'A3'))\n",
        " |      >>> mlatex(P.x)\n",
        " |      'A1'\n",
        " |  \n",
        " |  __iter__(self)\n",
        " |  \n",
        " |  __repr__ = __str__(self)\n",
        " |  \n",
        " |  __str__(self)\n",
        " |      Returns the name of the frame.\n",
        " |  \n",
        " |  ang_acc_in(self, otherframe)\n",
        " |      Returns the angular acceleration Vector of the ReferenceFrame.\n",
        " |      \n",
        " |      Effectively returns the Vector:\n",
        " |      ^N alpha ^B\n",
        " |      which represent the angular acceleration of B in N, where B is self, and\n",
        " |      N is otherframe.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      otherframe : ReferenceFrame\n",
        " |          The ReferenceFrame which the angular acceleration is returned in.\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> A = ReferenceFrame('A')\n",
        " |      >>> V = 10 * N.x\n",
        " |      >>> A.set_ang_acc(N, V)\n",
        " |      >>> A.ang_acc_in(N)\n",
        " |      10*N.x\n",
        " |  \n",
        " |  ang_vel_in(self, otherframe)\n",
        " |      Returns the angular velocity Vector of the ReferenceFrame.\n",
        " |      \n",
        " |      Effectively returns the Vector:\n",
        " |      ^N omega ^B\n",
        " |      which represent the angular velocity of B in N, where B is self, and\n",
        " |      N is otherframe.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      otherframe : ReferenceFrame\n",
        " |          The ReferenceFrame which the angular velocity is returned in.\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> A = ReferenceFrame('A')\n",
        " |      >>> V = 10 * N.x\n",
        " |      >>> A.set_ang_vel(N, V)\n",
        " |      >>> A.ang_vel_in(N)\n",
        " |      10*N.x\n",
        " |  \n",
        " |  dcm(self, otherframe)\n",
        " |      The direction cosine matrix between frames.\n",
        " |      \n",
        " |      This gives the DCM between this frame and the otherframe.\n",
        " |      The format is N.xyz = N.dcm(B) * B.xyz\n",
        " |      A SymPy Matrix is returned.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      otherframe : ReferenceFrame\n",
        " |          The otherframe which the DCM is generated to.\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> from sympy import symbols\n",
        " |      >>> q1 = symbols('q1')\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> A = N.orientnew('A', 'Axis', [q1, N.x])\n",
        " |      >>> N.dcm(A)\n",
        " |      [1,       0,        0]\n",
        " |      [0, cos(q1), -sin(q1)]\n",
        " |      [0, sin(q1),  cos(q1)]\n",
        " |  \n",
        " |  orient(self, parent, rot_type, amounts, rot_order='')\n",
        " |      Defines the orientation of this frame relative to a parent frame.\n",
        " |      \n",
        " |      Supported orientation types are Body, Space, Quaternion, Axis.\n",
        " |      Examples show correct usage.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      parent : ReferenceFrame\n",
        " |          The frame that this ReferenceFrame will have its orientation matrix\n",
        " |          defined in relation to.\n",
        " |      rot_type : str\n",
        " |          The type of orientation matrix that is being created.\n",
        " |      amounts : list OR value\n",
        " |          The quantities that the orientation matrix will be defined by.\n",
        " |      rot_order : str\n",
        " |          If applicable, the order of a series of rotations.\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> from sympy import symbols\n",
        " |      >>> q0, q1, q2, q3, q4 = symbols('q0 q1 q2 q3 q4')\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> B = ReferenceFrame('B')\n",
        " |      \n",
        " |      Now we have a choice of how to implement the orientation. First is\n",
        " |      Body. Body orientation takes this reference frame through three\n",
        " |      successive simple rotations. Acceptable rotation orders are of length\n",
        " |      3, expressed in XYZ or 123, and cannot have a rotation about about an\n",
        " |      axis twice in a row.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Body', [q1, q2, q3], '123')\n",
        " |      >>> B.orient(N, 'Body', [q1, q2, 0], 'ZXZ')\n",
        " |      >>> B.orient(N, 'Body', [0, 0, 0], 'XYX')\n",
        " |      \n",
        " |      Next is Space. Space is like Body, but the rotations are applied in the\n",
        " |      opposite order.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Space', [q1, q2, q3], '312')\n",
        " |      \n",
        " |      Next is Quaternion. This orients the new ReferenceFrame with\n",
        " |      Quaternions, defined as a finite rotation about lambda, a unit vector,\n",
        " |      by some amount theta.\n",
        " |      This orientation is described by four parameters:\n",
        " |      q0 = cos(theta/2)\n",
        " |      q1 = lambda_x sin(theta/2)\n",
        " |      q2 = lambda_y sin(theta/2)\n",
        " |      q3 = lambda_z sin(theta/2)\n",
        " |      Quaternion does not take in a rotation order.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Quaternion', [q0, q1, q2, q3])\n",
        " |      \n",
        " |      Last is Axis. This is a rotation about an arbitrary, non-time-varying\n",
        " |      axis by some angle. The axis is supplied as a Vector. This is how\n",
        " |      simple rotations are defined.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Axis', [q1, N.x + 2 * N.y])\n",
        " |  \n",
        " |  orientnew(self, newname, rot_type, amounts, rot_order='', indices=None, latexs=None)\n",
        " |      Creates a new ReferenceFrame oriented with respect to this Frame.\n",
        " |      \n",
        " |      See ReferenceFrame.orient() for acceptable rotation types, amounts,\n",
        " |      and orders. Parent is going to be self.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      newname : str\n",
        " |          The name for the new ReferenceFrame\n",
        " |      rot_type : str\n",
        " |          The type of orientation matrix that is being created.\n",
        " |      amounts : list OR value\n",
        " |          The quantities that the orientation matrix will be defined by.\n",
        " |      rot_order : str\n",
        " |          If applicable, the order of a series of rotations.\n",
        " |      \n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> from sympy import symbols\n",
        " |      >>> q1 = symbols('q1')\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> A = N.orientnew('A', 'Axis', [q1, N.x])\n",
        " |      \n",
        " |      \n",
        " |      .orient() documentation:\n",
        " |      \n",
        " |      ========================\n",
        " |      \n",
        " |      Defines the orientation of this frame relative to a parent frame.\n",
        " |      \n",
        " |      Supported orientation types are Body, Space, Quaternion, Axis.\n",
        " |      Examples show correct usage.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      parent : ReferenceFrame\n",
        " |          The frame that this ReferenceFrame will have its orientation matrix\n",
        " |          defined in relation to.\n",
        " |      rot_type : str\n",
        " |          The type of orientation matrix that is being created.\n",
        " |      amounts : list OR value\n",
        " |          The quantities that the orientation matrix will be defined by.\n",
        " |      rot_order : str\n",
        " |          If applicable, the order of a series of rotations.\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> from sympy import symbols\n",
        " |      >>> q0, q1, q2, q3, q4 = symbols('q0 q1 q2 q3 q4')\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> B = ReferenceFrame('B')\n",
        " |      \n",
        " |      Now we have a choice of how to implement the orientation. First is\n",
        " |      Body. Body orientation takes this reference frame through three\n",
        " |      successive simple rotations. Acceptable rotation orders are of length\n",
        " |      3, expressed in XYZ or 123, and cannot have a rotation about about an\n",
        " |      axis twice in a row.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Body', [q1, q2, q3], '123')\n",
        " |      >>> B.orient(N, 'Body', [q1, q2, 0], 'ZXZ')\n",
        " |      >>> B.orient(N, 'Body', [0, 0, 0], 'XYX')\n",
        " |      \n",
        " |      Next is Space. Space is like Body, but the rotations are applied in the\n",
        " |      opposite order.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Space', [q1, q2, q3], '312')\n",
        " |      \n",
        " |      Next is Quaternion. This orients the new ReferenceFrame with\n",
        " |      Quaternions, defined as a finite rotation about lambda, a unit vector,\n",
        " |      by some amount theta.\n",
        " |      This orientation is described by four parameters:\n",
        " |      q0 = cos(theta/2)\n",
        " |      q1 = lambda_x sin(theta/2)\n",
        " |      q2 = lambda_y sin(theta/2)\n",
        " |      q3 = lambda_z sin(theta/2)\n",
        " |      Quaternion does not take in a rotation order.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Quaternion', [q0, q1, q2, q3])\n",
        " |      \n",
        " |      Last is Axis. This is a rotation about an arbitrary, non-time-varying\n",
        " |      axis by some angle. The axis is supplied as a Vector. This is how\n",
        " |      simple rotations are defined.\n",
        " |      \n",
        " |      >>> B.orient(N, 'Axis', [q1, N.x + 2 * N.y])\n",
        " |  \n",
        " |  set_ang_acc(self, otherframe, value)\n",
        " |      Define the angular acceleration Vector in a ReferenceFrame.\n",
        " |      \n",
        " |      Defines the angular acceleration of this ReferenceFrame, in another.\n",
        " |      Angular acceleration can be defined with respect to multiple different\n",
        " |      ReferenceFrames. Care must be taken to not create loops which are\n",
        " |      inconsistent.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      otherframe : ReferenceFrame\n",
        " |          A ReferenceFrame to define the angular acceleration in\n",
        " |      value : Vector\n",
        " |          The Vector representing angular acceleration\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> A = ReferenceFrame('A')\n",
        " |      >>> V = 10 * N.x\n",
        " |      >>> A.set_ang_acc(N, V)\n",
        " |      >>> A.ang_acc_in(N)\n",
        " |      10*N.x\n",
        " |  \n",
        " |  set_ang_vel(self, otherframe, value)\n",
        " |      Define the angular velocity vector in a ReferenceFrame.\n",
        " |      \n",
        " |      Defines the angular velocity of this ReferenceFrame, in another.\n",
        " |      Angular velocity can be defined with respect to multiple different\n",
        " |      ReferenceFrames. Care must be taken to not create loops which are\n",
        " |      inconsistent.\n",
        " |      \n",
        " |      Parameters\n",
        " |      ==========\n",
        " |      \n",
        " |      otherframe : ReferenceFrame\n",
        " |          A ReferenceFrame to define the angular velocity in\n",
        " |      value : Vector\n",
        " |          The Vector representing angular velocity\n",
        " |      \n",
        " |      Examples\n",
        " |      ========\n",
        " |      \n",
        " |      >>> from sympy.physics.mechanics import ReferenceFrame, Vector\n",
        " |      >>> N = ReferenceFrame('N')\n",
        " |      >>> A = ReferenceFrame('A')\n",
        " |      >>> V = 10 * N.x\n",
        " |      >>> A.set_ang_vel(N, V)\n",
        " |      >>> A.ang_vel_in(N)\n",
        " |      10*N.x\n",
        " |  \n",
        " |  ----------------------------------------------------------------------\n",
        " |  Data descriptors defined here:\n",
        " |  \n",
        " |  __dict__\n",
        " |      dictionary for instance variables (if defined)\n",
        " |  \n",
        " |  __weakref__\n",
        " |      list of weak references to the object (if defined)\n",
        " |  \n",
        " |  x\n",
        " |      The basis Vector for the ReferenceFrame, in the x direction.\n",
        " |  \n",
        " |  y\n",
        " |      The basis Vector for the ReferenceFrame, in the y direction.\n",
        " |  \n",
        " |  z\n",
        " |      The basis Vector for the ReferenceFrame, in the z direction.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The `ReferenceFrame` class manages everything about rotations, angular velocities, and angular accelerations with respect to other reference frames. Now create an inertial reference frame called N for \"Newtonian\" as was described in the the docstring:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N = ReferenceFrame('N')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Keep in mind that `N` is the variable name of which the reference frame named \"N\"\n",
      "is stored. It is important to note that `N` is an object and it has properties\n",
      "and functions associated with it. To see a list of them type:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dir(N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "['__class__',\n",
        " '__delattr__',\n",
        " '__dict__',\n",
        " '__doc__',\n",
        " '__format__',\n",
        " '__getattribute__',\n",
        " '__getitem__',\n",
        " '__hash__',\n",
        " '__init__',\n",
        " '__iter__',\n",
        " '__module__',\n",
        " '__new__',\n",
        " '__reduce__',\n",
        " '__reduce_ex__',\n",
        " '__repr__',\n",
        " '__setattr__',\n",
        " '__sizeof__',\n",
        " '__str__',\n",
        " '__subclasshook__',\n",
        " '__weakref__',\n",
        " '_ang_acc_dict',\n",
        " '_ang_vel_dict',\n",
        " '_cur',\n",
        " '_dcm_dict',\n",
        " '_dict_list',\n",
        " '_dlist',\n",
        " '_w_diff_dcm',\n",
        " '_x',\n",
        " '_y',\n",
        " '_z',\n",
        " 'ang_acc_in',\n",
        " 'ang_vel_in',\n",
        " 'dcm',\n",
        " 'indices',\n",
        " 'latex_vecs',\n",
        " 'name',\n",
        " 'orient',\n",
        " 'orientnew',\n",
        " 'pretty_vecs',\n",
        " 'set_ang_acc',\n",
        " 'set_ang_vel',\n",
        " 'str_vecs',\n",
        " 'x',\n",
        " 'y',\n",
        " 'z']"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Notice that three of the properties are `x`, `y`, and `z`. These are the\n",
      "orthonormal unit vectors associated with the reference frame and are the\n",
      "building blocks for creating more general vectors. We can create a vector by simply\n",
      "building a linear combination of the unit vectors:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v = 1 * N.x + 2 * N.y + 3 * N.z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now a vector expressed in the N reference frame is stored in the variable `v`.\n",
      "We can print `v` to the screen by typing:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(v)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "N.x + 2*N.y + 3*N.z\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The vector `v` can be manipulated as expected. You can multiply and divide them by scalars:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "2 * v"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "2*N.x + 4*N.y + 6*N.z"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v / 3.0"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "0.333333333333333*N.x + 0.666666666666667*N.y + N.z"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Note that three is expressed as `3.0` instead of `3`. The python language does\n",
      "integer division by default. There are ways around this, but for now simply\n",
      "remember to always declare numbers as floats (i.e. include a decimal).\n",
      "\n",
      "You can also add and subtract vectors::"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v + v"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "2*N.x + 4*N.y + 6*N.z"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "w = 5 * N.x + 7 * N.y"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v - w"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "- 4*N.x - 5*N.y + 3*N.z"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Vectors also have some useful properties:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dir(v)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "['__add__',\n",
        " '__and__',\n",
        " '__class__',\n",
        " '__delattr__',\n",
        " '__dict__',\n",
        " '__div__',\n",
        " '__doc__',\n",
        " '__eq__',\n",
        " '__format__',\n",
        " '__getattribute__',\n",
        " '__hash__',\n",
        " '__init__',\n",
        " '__module__',\n",
        " '__mul__',\n",
        " '__ne__',\n",
        " '__neg__',\n",
        " '__new__',\n",
        " '__or__',\n",
        " '__radd__',\n",
        " '__rand__',\n",
        " '__reduce__',\n",
        " '__reduce_ex__',\n",
        " '__repr__',\n",
        " '__rmul__',\n",
        " '__ror__',\n",
        " '__rsub__',\n",
        " '__setattr__',\n",
        " '__sizeof__',\n",
        " '__str__',\n",
        " '__sub__',\n",
        " '__subclasshook__',\n",
        " '__truediv__',\n",
        " '__weakref__',\n",
        " '__xor__',\n",
        " '_latex',\n",
        " '_pretty',\n",
        " '_sympyrepr',\n",
        " '_sympystr',\n",
        " 'args',\n",
        " 'cross',\n",
        " 'diff',\n",
        " 'doit',\n",
        " 'dot',\n",
        " 'dt',\n",
        " 'express',\n",
        " 'magnitude',\n",
        " 'normalize',\n",
        " 'outer',\n",
        " 'simp',\n",
        " 'simplify',\n",
        " 'subs']"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "You can find the magnitude of a vector by typing:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v.magnitude()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "sqrt(14)"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "You can compute a unit vector in the direction of `v`:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v.normalize()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "sqrt(14)/14*N.x + sqrt(14)/7*N.y + 3*sqrt(14)/14*N.z"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "You can find the measure numbers and the reference frame the vector was defined in\n",
      "with::"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v.args"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "[([1]\n",
        "[2]\n",
        "[3], N)]"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Dot and cross products of vectors can also be computed::"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dot(v, w)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 17,
       "text": [
        "19"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cross(v, w)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 18,
       "text": [
        "- 21*N.x + 15*N.y - 3*N.z"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We've only used numbers as our measure numbers so far, but it is just as easy\n",
      "to use symbols. We will introduce six symbols for our measure numbers with the\n",
      "SymPy `symbols` [`help(symbols) for the documentation`] function:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a1, a2, a3 = symbols('a1 a2 a3')\n",
      "b1, b2, b3 = symbols('b1 b2 b3')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 19
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "And create two new vectors that are completely symbolic:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = a1 * N.x + a2 * N.y + a3 * N.z\n",
      "y = b1 * N.x + b2 * N.y + b3 * N.z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dot(x, y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 21,
       "text": [
        "a1*b1 + a2*b2 + a3*b3"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "z = cross(x, y)\n",
      "z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 22,
       "text": [
        "(a2*b3 - a3*b2)*N.x + (-a1*b3 + a3*b1)*N.y + (a1*b2 - a2*b1)*N.z"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Numbers and symbols work together seamlessly:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dot(v, x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 23,
       "text": [
        "a1 + 2*a2 + 3*a3"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "You can also differentiate a vector with respect to a variable in a\n",
      "reference frame:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "z.diff(a1, N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 24,
       "text": [
        "- b3*N.y + b2*N.z"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Vectors don't have be defined with respect to just one reference frame. We can\n",
      "create a new reference frame and orient it with respect to the `N` frame that\n",
      "has already been created. We will use the `orient` method of the new frame to\n",
      "do a simple rotation through `alpha` about the `N.x` axis:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A = ReferenceFrame('A')\n",
      "alpha = symbols('alpha')\n",
      "A.orient(N, 'Axis', [alpha, N.x])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 25
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now the direction cosine matrix with of `A` with respect to `N` can be\n",
      "computed:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A.dcm(N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 26,
       "text": [
        "[1,           0,          0]\n",
        "[0,  cos(alpha), sin(alpha)]\n",
        "[0, -sin(alpha), cos(alpha)]"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now that SymPy knows that `A` and `N` are oriented with respect to each other\n",
      "we can express the vectors that we originally wrote in the `A` frame:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "v.express(A)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "A.x + (3*sin(alpha) + 2*cos(alpha))*A.y + (-2*sin(alpha) + 3*cos(alpha))*A.z"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "z.express(A)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 28,
       "text": [
        "(a2*b3 - a3*b2)*A.x + ((a1*b2 - a2*b1)*sin(alpha) + (-a1*b3 + a3*b1)*cos(alpha))*A.y + ((a1*b2 - a2*b1)*cos(alpha) - (-a1*b3 + a3*b1)*sin(alpha))*A.z"
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In dynamics systems, at least some of the relative orientation of reference\n",
      "frames and vectors are time varying. The mechanics module provides a way to\n",
      "specify quantities as time varying. Let's define two variables `beta` and `d` as\n",
      "variables which are functions of time:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "beta, d = dynamicsymbols('beta d')\n",
      "beta, d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 29,
       "text": [
        "(beta(t), d(t))"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now we can create a new reference frame that is oriented with respect to the `A`\n",
      "frame by `beta` and create a vector in that new frame that is a function of `d`.\n",
      "This time we will use the `orientnew` method of the `A` frame to create the new\n",
      "reference frame `B`:\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "B = A.orientnew('B', 'Axis', (beta, A.y))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 30
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "vec = d * B.z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 31
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We can now compute the angular velocity of the reference frame `B` with respect\n",
      "to other reference frames:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "B.ang_vel_in(N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "beta'*A.y"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This allows us to now differentiate the vector, `vec`, with respect to time and\n",
      "a reference frame::"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "vecdot = vec.dt(N)\n",
      "vecdot"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 33,
       "text": [
        "d*beta'*B.x + d'*B.z"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "vecdot.express(N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 34,
       "text": [
        "(d*cos(beta)*beta' + sin(beta)*d')*N.x + (d*sin(alpha)*sin(beta)*beta' - sin(alpha)*cos(beta)*d')*N.y + (-d*sin(beta)*cos(alpha)*beta' + cos(alpha)*cos(beta)*d')*N.z"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The `dynamicsymbols` function also allows you to easily create the derivatives of time\n",
      "varying variables and store them in a variable."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "theta = dynamicsymbols('theta')\n",
      "thetad = dynamicsymbols('theta', 1)\n",
      "theta, thetad"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 35,
       "text": [
        "(theta(t), Derivative(theta(t), t))"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "At this point we now have all the tools needed to setup the kinematics for a\n",
      "dynamic system. Let's start with the classic mass spring damper system under the influence of gravity. Go to the next notebook to follow along: https://github.com/PythonDynamics/pydy_examples/tree/master/mass_spring_damper."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}